home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue27 / whoami / VERINFO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-28  |  6.7 KB  |  238 lines

  1. unit VerInfo;
  2.  
  3. interface
  4.  
  5. uses SysUtils, WinTypes, Dialogs, Classes;
  6.  
  7. type
  8.   { define a generic exception class for version info, and an exception
  9.     to indicate that no version info is available. }
  10.   EVerInfoError   = class(Exception);
  11.   ENoVerInfoError = class(Exception);
  12.   eNoFixeVerInfo  = class(Exception);
  13.  
  14.   // define enum type representing different types of version info
  15.   TVerInfoType =
  16.     (viCompanyName,
  17.      viFileDescription,
  18.      viFileVersion,
  19.      viInternalName,
  20.      viLegalCopyright,
  21.      viLegalTrademarks,
  22.      viOriginalFilename,
  23.      viProductName,
  24.      viProductVersion,
  25.      viComments);
  26.  
  27. const
  28.  
  29.   // define an array constant of strings representing the pre-defined
  30.   // version information keys.
  31.   VerNameArray: array[viCompanyName..viComments] of String[20] =
  32.   ('CompanyName',
  33.    'FileDescription',
  34.    'FileVersion',
  35.    'InternalName',
  36.    'LegalCopyright',
  37.    'LegalTrademarks',
  38.    'OriginalFilename',
  39.    'ProductName',
  40.    'ProductVersion',
  41.    'Comments');
  42.  
  43. type
  44.  
  45.   // Define the version info class
  46.   TVerInfoRes = class
  47.   private
  48.     Handle            : DWord;
  49.     Size              : Integer;
  50.     RezBuffer         : String;
  51.     TransTable        : PLongint;
  52.     FixedFileInfoBuf  : PVSFixedFileInfo;
  53.     FFileFlags        : TStringList;
  54.     FFileName         : String;
  55.     procedure FillFixedFileInfoBuf;
  56.     procedure FillFileVersionInfo;
  57.     procedure FillFileMaskInfo;
  58.   protected
  59.     function GetFileVersion   : String;
  60.     function GetProductVersion: String;
  61.     function GetFileOS        : String;
  62.   public
  63.     constructor Create(aFileName: String);
  64.     destructor Destroy; override;
  65.     function GetPreDefKeyString(aVerKind: TVerInfoType): String;
  66.     function GetUserDefKeyString(aKey: String): String;
  67.     property FileVersion    : String read GetFileVersion;
  68.     property ProductVersion : String read GetProductVersion;
  69.     property FileFlags      : TStringList read FFileFlags;
  70.     property FileOS         : String read GetFileOS;
  71.   end;
  72.  
  73. implementation
  74.  
  75. uses Windows;
  76.  
  77. const
  78.   // strings that must be fed to VerQueryValue() function
  79.   SFInfo = '\StringFileInfo\';
  80.   VerTranslation: PChar = '\VarFileInfo\Translation';
  81.   FormatStr = '%s%.4x%.4x\%s%s';
  82.  
  83.  
  84. constructor TVerInfoRes.Create(aFileName: String);
  85. begin
  86.   FFileName := aFileName;
  87.   FFileFlags := TStringList.Create;
  88.   // Get the file version information
  89.   FillFileVersionInfo;
  90.   //Get the fixed file info
  91.   FillFixedFileInfoBuf;
  92.   // Get the file mask values
  93.   FillFileMaskInfo;
  94. end;
  95.  
  96.  
  97. destructor TVerInfoRes.Destroy;
  98. begin
  99.   FFileFlags.Free;
  100. end;
  101.  
  102. procedure TVerInfoRes.FillFileVersionInfo;
  103. var
  104.   SBSize: UInt;
  105. begin
  106.   { Determine size of version information }
  107.   Size := GetFileVersionInfoSize(PChar(FFileName), Handle);
  108.   if Size <= 0 then         { raise exception if size <= 0 }
  109.     raise ENoVerInfoError.Create('No Version Info Available.');
  110.  
  111.   // Set the length accordingly
  112.   SetLength(RezBuffer, Size);
  113.   // Fill the buffer with version information, raise exception on error
  114.   if not GetFileVersionInfo(PChar(FFileName), Handle, Size, PChar(RezBuffer)) then
  115.     raise EVerInfoError.Create('Cannot obtain version info.');
  116.  
  117.   // Get translation info, raise exception on error
  118.   if not VerQueryValue(PChar(RezBuffer), VerTranslation,  pointer(TransTable),
  119.   SBSize) then
  120.     raise EVerInfoError.Create('No language info.');
  121. end;
  122.  
  123. procedure TVerInfoRes.FillFixedFileInfoBuf;
  124. var
  125.   Size: Longint;
  126. begin
  127.   if VerQueryValue(PChar(RezBuffer), '\', pointer(FixedFileInfoBuf), Size) then begin
  128.      if Size < SizeOf(TVSFixedFileInfo) then
  129.         raise eNoFixeVerInfo.Create('No fixed file info');
  130.   end
  131.   else
  132.     raise eNoFixeVerInfo.Create('No fixed file info')
  133. end;
  134.  
  135. procedure TVerInfoRes.FillFileMaskInfo;
  136. begin
  137.   with FixedFileInfoBuf^ do begin
  138.     if (dwFileFlagsMask and dwFileFlags and VS_FF_PRERELEASE) <> 0then
  139.       FFileFlags.Add('Pre-release');
  140.     if (dwFileFlagsMask and dwFileFlags and VS_FF_PRIVATEBUILD) <> 0 then
  141.       FFileFlags.Add('Private build');
  142.     if (dwFileFlagsMask and dwFileFlags and VS_FF_SPECIALBUILD) <> 0 then
  143.       FFileFlags.Add('Special build');
  144.     if (dwFileFlagsMask and dwFileFlags and VS_FF_DEBUG) <> 0 then
  145.       FFileFlags.Add('Debug');
  146.   end;
  147. end;
  148.  
  149. function TVerInfoRes.GetPreDefKeyString(aVerKind: TVerInfoType): String;
  150. var
  151.   P: PChar;
  152.   S: UInt;
  153. begin
  154.   Result := Format(FormatStr, [SfInfo, LoWord(TransTable^),HiWord(TransTable^),
  155.     VerNameArray[aVerKind], #0]);
  156.   // get and return version query info, return empty string on error
  157.   if VerQueryValue(PChar(RezBuffer), @Result[1], Pointer(P), S) then
  158.     Result := StrPas(P)
  159.   else
  160.     Result := '';
  161. end;
  162.  
  163. function TVerInfoRes.GetUserDefKeyString(aKey: String): String;
  164. var
  165.   P: Pchar;
  166.   S: UInt;
  167. begin
  168.   Result := Format(FormatStr, [SfInfo, LoWord(TransTable^),HiWord(TransTable^),
  169.     aKey, #0]);
  170.   // get and return version query info, return empty string on error
  171.   if VerQueryValue(PChar(RezBuffer), @Result[1], Pointer(P), S) then
  172.     Result := StrPas(P)
  173.   else
  174.     Result := '';
  175. end;
  176.  
  177.  
  178. function VersionString(Ms, Ls: Longint): String;
  179. begin
  180.   Result := Format('%d.%d.%d.%d', [HIWORD(Ms), LOWORD(Ms),
  181.      HIWORD(Ls), LOWORD(Ls)]);
  182. end;
  183.  
  184. function TVerInfoRes.GetFileVersion: String;
  185. begin
  186.   with FixedFileInfoBuf^ do
  187.     Result := VersionString(dwFileVersionMS, dwFileVersionLS);
  188. end;
  189.  
  190. function TVerInfoRes.GetProductVersion: String;
  191. begin
  192.   with FixedFileInfoBuf^ do
  193.     Result := VersionString(dwProductVersionMS, dwProductVersionLS);
  194. end;
  195.  
  196. function TVerInfoRes.GetFileOS: String;
  197. begin
  198.   with FixedFileInfoBuf^ do
  199.     case dwFileOS of
  200.       VOS_UNKNOWN:  // Same as VOS__BASE
  201.         Result := 'Unknown';
  202.       VOS_DOS:
  203.         Result := 'Designed for MS-DOS';
  204.       VOS_OS216:
  205.         Result := 'Designed for 16-bit OS/2';
  206.       VOS_OS232:
  207.         Result := 'Designed for 32-bit OS/2';
  208.       VOS_NT:
  209.         Result := 'Designed for Windows NT';
  210.  
  211.  
  212.       VOS__WINDOWS16:
  213.         Result := 'Designed for 16-bit Windows';
  214.       VOS__PM16:
  215.         Result := 'Designed for 16-bit PM';
  216.       VOS__PM32:
  217.         Result := 'Designed for 32-bit PM';
  218.       VOS__WINDOWS32:
  219.         Result := 'Designed for 32-bit Windows';
  220.  
  221.       VOS_DOS_WINDOWS16:
  222.         Result := 'Designed for 16-bit Windows, running on MS-DOS';
  223.       VOS_DOS_WINDOWS32:
  224.         Result := 'Designed for Win32 API, running on MS-DOS';
  225.       VOS_OS216_PM16:
  226.         Result := 'Designed for 16-bit PM, running on 16-bit OS/2';
  227.       VOS_OS232_PM32:
  228.         Result := 'Designed for 32-bit PM, running on 32-bit OS/2';
  229.       VOS_NT_WINDOWS32:
  230.         Result := 'Designed for Win32 API, running on Windows/NT';
  231.     else
  232.       Result := 'Unknown';
  233.     end;
  234. end;
  235.  
  236.  
  237. end.
  238.